import "./index.less";
import { BasicTable, useTable, BasicColumn, FormSchema, useRender } from "@/components/Table";
import Toolbar from "@/components/ViewComponents/Toolbar";
import { StockRecordApi } from "@/api/erp/stock/record";
import { productApi } from "@/api/erp/drug/drug";
import { WarehouseApi } from "@/api/erp/stock/warehouse";
import { exportToExcel } from "@/utils/util";
import { useTranslation } from "react-i18next";

const ErpStockRecord = () => {
	const en_vi = "drugStorage.inventoryManagement";
	const { t } = useTranslation();
	const getIntDictOptions = () => {
		return [
			{
				label: t(`${en_vi}.inventoryDetails.otherInboundItems`),
				value: 10
			},
			{
				label: t(`${en_vi}.inventoryDetails.otherInventory`),
				value: 11
			},
			{
				label: t(`${en_vi}.inventoryDetails.otherOutboundShipments`),
				value: 20
			},
			{
				label: t(`${en_vi}.inventoryDetails.otherOutbound`),
				value: 21
			},
			{
				label: t(`${en_vi}.inventoryDetails.transferInventory`),
				value: 30
			},
			{
				label: t(`${en_vi}.inventoryDetails.transferInventoryVoid`),
				value: 31
			},
			{
				label: t(`${en_vi}.inventoryDetails.transferOutbound`),
				value: 32
			},
			{
				label: t(`${en_vi}.inventoryDetails.transferOutboundVoid`),
				value: 33
			}
		];
	};
	const formSchemaTop: FormSchema[] = [
		{
			label: t("drugStorage.productName"),
			component: "ApiSelect",
			field: "productId",
			componentProps: {
				api: productApi.getproductSimpleList,
				labelField: "productName",
				valueField: "productId",
				immediate: false,
				params: {
					bizType: "4"
				}
			}
		},
		{
			label: t(`${en_vi}.drugInventory.warehouseName`),
			component: "ApiSelect",
			field: "whsId",
			componentProps: {
				api: WarehouseApi.getWarehouseSimpleList,
				labelField: "name",
				valueField: "whsId",
				immediate: false
			}
		},
		{
			label: t(`${en_vi}.inventoryDetails.bizType`),
			component: "Select",
			field: "bizType",
			componentProps: {
				options: getIntDictOptions().map(item => ({ ...item, label: item.label, value: item.value }))
			}
		},
		{
			label: t(`${en_vi}.inventoryDetails.bizNo`),
			component: "Input",
			field: "bizNo"
		}
	];
	// 配置table
	const columns: BasicColumn[] = [
		{
			title: t("drugStorage.productName"),
			dataIndex: "productName"
		},
		{
			title: t(`${en_vi}.drugInventory.cateName`),
			dataIndex: "cateName"
		},
		{
			title: t(`${en_vi}.drugInventory.unitName`),
			dataIndex: "unitName"
		},
		{
			title: t(`drugStorage.inventoryManagement.drugInventory.warehouseName`),
			dataIndex: "warehouseName"
		},
		{
			title: t(`${en_vi}.inventoryDetails.bizNo`),
			dataIndex: "bizNo"
		},
		{
			title: t(`${en_vi}.drugInventory.count`),
			dataIndex: "count",
			render: text => parseFloat(text).toFixed(3)
		},
		{
			title: t("felix_ui.createdTime"),
			dataIndex: "createdTime",
			render: text => useRender.renderDate(text)
		},
		{
			title: t(`${en_vi}.inventoryDetails.totalCount`),
			dataIndex: "totalCount",
			render: text => parseFloat(text).toFixed(3)
		},
		{
			title: t(`${en_vi}.inventoryDetails.createdBy`),
			dataIndex: "createdByName"
		}
	];
	// 配置table
	const [registerTable] = useTable({
		api: StockRecordApi.getStockRecordPage,
		columns,
		formConfig: {
			labelWidth: 130,
			schemas: formSchemaTop,
			showActionButtonGroup: true,
			showAdvancedButton: false,
			baseColProps: { span: 6 },
			actionColOptions: { span: 24 }
		},
		bordered: true,
		useSearchForm: true,
		searchInfo: {
			bizTypeWhm: "4"
		}
	});

	//表格导出
	const exportExcel = async () => {
		await StockRecordApi.getStockRecordPage({}).then(res => {
			exportToExcel(res.list, t("felix_ui.pat_basic_info"));
		});
	};
	const handlerToolbarAction = async (key: string) => {
		switch (key) {
			case "export":
				exportExcel();
				break;
		}
	};
	return (
		<div className="page">
			<div className="page-content">
				<BasicTable register={registerTable}></BasicTable>
			</div>
			<Toolbar action={handlerToolbarAction} btnList={["export"]} />
		</div>
	);
};
export default ErpStockRecord;
